#include "EepromModule.h"
#include "I2cInitHelper.h"

#include <string.h>

Eeprom_Ctrl_Def eeprom_fram;

// 读EEPROM地址
rt_err_t Read_Eeprom_Bytes_Interface( Eeprom_Ctrl_Def* obj, uint32_t address, uint8_t *data_buf, size_t data_len ) {
	rt_err_t res = RT_EOK;
  // 锁定资源。
  if (strlen(obj->eeprom_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
    uint8_t get_mutex = 0;
    while (rt_mutex_take(&(obj->eeprom_lock), 50 * (2 ^ get_mutex)) != RT_EOK) {
      if (get_mutex >= 3) {
        return RT_EBUSY;
      }
      get_mutex++;
    }
  }
	
	rt_memset(obj->i2c_ctrl_obj.data_buf, 0 , sizeof(obj->i2c_ctrl_obj.data_buf));
	obj->i2c_ctrl_obj.send_data_len = 2;
	obj->i2c_ctrl_obj.data_buf[1] = (address >> 8) % 0x100;
	obj->i2c_ctrl_obj.data_buf[2] = address % 0x100;
	obj->i2c_ctrl_obj.receive_data_len = data_len;
	res = I2C_Master_Translate_IT(obj->i2c_ctrl_obj.i2c_cm, &(obj->i2c_ctrl_obj), obj->addr);
	
	if ( NULL != data_buf ){
		rt_memcpy(data_buf, obj->i2c_ctrl_obj.data_buf + 1 + obj->i2c_ctrl_obj.send_data_len, obj->i2c_ctrl_obj.receive_data_len);
	}
	
	// 释放总线资源
	if (strlen(obj->eeprom_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
    rt_mutex_release(&(obj->eeprom_lock));
  }
  return res;
} // 读EEPROM地址

// 写EEPROM地址数据
rt_err_t Write_Eeprom_Bytes_Interface( Eeprom_Ctrl_Def* obj, uint32_t address, uint8_t *data_buf, size_t data_len ) {
	rt_err_t res = RT_EOK;
  // 锁定资源。
  if (strlen(obj->eeprom_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
    uint8_t get_mutex = 0;
    while (rt_mutex_take(&(obj->eeprom_lock), 50 * (2 ^ get_mutex)) != RT_EOK) {
      if (get_mutex >= 3) {
        return RT_EBUSY;
      }
      get_mutex++;
    }
  }
	
	rt_memset(obj->i2c_ctrl_obj.data_buf, 0 , sizeof(obj->i2c_ctrl_obj.data_buf));
	obj->i2c_ctrl_obj.send_data_len = 2 + data_len;
	obj->i2c_ctrl_obj.data_buf[1] = (address >> 8) % 0x100;
	obj->i2c_ctrl_obj.data_buf[2] = address % 0x100;
	for (int index=0; index<data_len; index++){
		obj->i2c_ctrl_obj.data_buf[3 + index] = data_buf[index];
	}
	obj->i2c_ctrl_obj.receive_data_len = 0;
	res = I2C_Master_Translate_IT(obj->i2c_ctrl_obj.i2c_cm, &(obj->i2c_ctrl_obj), obj->addr);

	// 释放总线资源
	if (strlen(obj->eeprom_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
    rt_mutex_release(&(obj->eeprom_lock));
  }
  return res;
} // 写EEPROM地址数据

void INT_SRC_EEPROM_I2C_RXI_IrqCallback(void) {
  INT_SRC_I2Cx_RXI_IrqCallback(eeprom_fram.i2c_ctrl_obj.i2c_cm, &(eeprom_fram.i2c_ctrl_obj));
}

void INT_SRC_EEPROM_I2C_TXI_IrqCallback(void) {
  INT_SRC_I2Cx_TXI_IrqCallback(eeprom_fram.i2c_ctrl_obj.i2c_cm, &(eeprom_fram.i2c_ctrl_obj));
}

void INT_SRC_EEPROM_I2C_TEI_IrqCallback(void) {
  INT_SRC_I2Cx_TEI_IrqCallback(eeprom_fram.i2c_ctrl_obj.i2c_cm, &(eeprom_fram.i2c_ctrl_obj));
}

void INT_SRC_EEPROM_I2C_EEI_IrqCallback(void) {
  INT_SRC_I2Cx_EEI_IrqCallback(eeprom_fram.i2c_ctrl_obj.i2c_cm, &(eeprom_fram.i2c_ctrl_obj));
}


// 初始化EEPROM
rt_err_t Init_Eeprom (void) {
  Init_I2c_Data_Task_Obj(&(eeprom_fram.i2c_ctrl_obj), "eeprom_i2c", CM_I2C4);
	rt_mutex_init(&(eeprom_fram.eeprom_lock), "eeprom_feram_device_lock", RT_IPC_FLAG_PRIO);
	
	eeprom_fram.arloie_try = 0;
	eeprom_fram.addr = EEPROM1_ADDR;
	
	eeprom_fram.Read_Eeprom_Bytes = Read_Eeprom_Bytes_Interface;
	eeprom_fram.Write_Eeprom_Bytes = Write_Eeprom_Bytes_Interface;
	
	return RT_EOK;
} // 初始化EEPROM
